<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,200分)- 探索地块建立（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给一块n*m的地块&#xff0c;相当于n*m的二维数组&#xff0c;每个元素的值表示这个小地块的发电量&#xff1b;</p> 
<p>求在这块地上建立正方形的边长为c的发电站&#xff0c;发电量满足目标电量k的地块数量。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行为四个按空格分隔的正整数&#xff0c;分别表示n, m , c k</p> 
<p>后面n行整数&#xff0c;表示每个地块的发电量</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出满足条件的地块数量</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2 5 2 6<br /> 1 3 4 5 8<br /> 2 3 6 7 1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">4</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题最优解题思路是使用&#xff1a;二维矩阵前缀</p> 
<p>关于二维矩阵前缀和&#xff0c;请看<a href="https://fcqian.blog.csdn.net/article/details/128976936?spm&#61;1001.2014.3001.5502" rel="nofollow" title="算法设计 - 前缀和 &amp; 差分数列_伏城之外的博客-CSDN博客">算法设计 - 前缀和 &amp; 差分数列_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<h4>JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n, m, c, k;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    [n, m, c, k] &#61; lines[0].split(&#34; &#34;).map(Number);
  }

  if (n &amp;&amp; lines.length &#61;&#61;&#61; n &#43; 1) {
    const matrix &#61; lines.slice(1).map((line) &#61;&gt; line.split(&#34; &#34;).map(Number));

    console.log(getResult(matrix, n, m, c, k));

    lines.length &#61; 0;
  }
});

/**
 *
 * &#64;param {*} matrix n*m的地块
 * &#64;param {*} n 地块行数
 * &#64;param {*} m 地块列数
 * &#64;param {*} c 正方形的发电站边长为c
 * &#64;param {*} k 目标电量k
 */
function getResult(matrix, n, m, c, k) {
  const preSum &#61; new Array(n &#43; 1).fill(0).map(() &#61;&gt; new Array(m &#43; 1).fill(0));

  for (let i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
    for (let j &#61; 1; j &lt;&#61; m; j&#43;&#43;) {
      preSum[i][j] &#61;
        preSum[i - 1][j] &#43;
        preSum[i][j - 1] -
        preSum[i - 1][j - 1] &#43;
        matrix[i - 1][j - 1];
    }
  }

  let ans &#61; 0;

  for (let i &#61; c; i &lt;&#61; n; i&#43;&#43;) {
    for (let j &#61; c; j &lt;&#61; m; j&#43;&#43;) {
      const square &#61;
        preSum[i][j] -
        (preSum[i - c][j] &#43; preSum[i][j - c]) &#43;
        preSum[i - c][j - c];

      if (square &gt;&#61; k) ans&#43;&#43;;
    }
  }

  return ans;
}
</code></pre> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();
    int m &#61; sc.nextInt();
    int c &#61; sc.nextInt();
    int k &#61; sc.nextInt();

    int[][] matrix &#61; new int[n][m];

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
        matrix[i][j] &#61; sc.nextInt();
      }
    }

    System.out.println(getResult(matrix, n, m, c, k));
  }

  /**
   * &#64;param matrix n*m的地块
   * &#64;param n 地块行数
   * &#64;param m 地块列数
   * &#64;param c 正方形的发电站边长为c
   * &#64;param k 目标电量k
   * &#64;return 可以建设几个发电站
   */
  public static int getResult(int[][] matrix, int n, int m, int c, int k) {
    int[][] preSum &#61; new int[n &#43; 1][m &#43; 1];

    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
      for (int j &#61; 1; j &lt;&#61; m; j&#43;&#43;) {
        preSum[i][j] &#61;
            preSum[i - 1][j] &#43; preSum[i][j - 1] - preSum[i - 1][j - 1] &#43; matrix[i - 1][j - 1];
      }
    }

    int ans &#61; 0;

    for (int i &#61; c; i &lt;&#61; n; i&#43;&#43;) {
      for (int j &#61; c; j &lt;&#61; m; j&#43;&#43;) {
        int square &#61; preSum[i][j] - (preSum[i - c][j] &#43; preSum[i][j - c]) &#43; preSum[i - c][j - c];
        if (square &gt;&#61; k) ans&#43;&#43;;
      }
    }

    return ans;
  }
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n, m, c, k &#61; map(int, input().split())
matrix &#61; [list(map(int, input().split())) for i in range(n)]


# 算法入口
def getResult(n, m, c, k, matrix):
    &#34;&#34;&#34;
    :param n: 调研区域的长&#xff0c;行数
    :param m: 调研区域的宽&#xff0c;列数
    :param c: 正方形电站的边长
    :param k: 正方形电站的最低发电量
    :param matrix: 调研区域每单位面积的发电量矩阵
    :return: 返回调研区域有几个符合要求正方形电站
    &#34;&#34;&#34;

    preSum &#61; [[0 for j in range(m &#43; 1)] for i in range(n &#43; 1)]

    for i in range(1, n &#43; 1):
        for j in range(1, m &#43; 1):
            preSum[i][j] &#61; preSum[i - 1][j] &#43; preSum[i][j - 1] - preSum[i - 1][j - 1] &#43; matrix[i - 1][j - 1]

    ans &#61; 0

    for i in range(c, n &#43; 1):
        for j in range(c, m &#43; 1):
            square &#61; preSum[i][j] - (preSum[i - c][j] &#43; preSum[i][j - c]) &#43; preSum[i - c][j - c]
            if square &gt;&#61; k:
                ans &#43;&#61; 1

    return ans


# 算法调用
print(getResult(n, m, c, k, matrix))
</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>